# -*- coding: utf-8 -*-

"""
Module implementing frmSpider.
"""

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget, QApplication
from Ui_spiderFund import Ui_Form
import sys
from sql.excuteSQL import OprationSQLite
from ThreadSpider import spiderFundThread
from showData import showTableWidgets

class frmSpider(QWidget, Ui_Form):
    """
    Class documentation goes here.
    """

    def __init__(self, parent=None):
        """
        Constructor

        @param parent reference to the parent widget
        @type QWidget
        """
        super(frmSpider, self).__init__(parent)
        self.setupUi(self)
        self.ThreadRead=spiderFundThread('scrapy crawl eastmoney')
        self.ThreadRead.postSignal.connect(self.slotAdd)

    @pyqtSlot()
    def on_btnspider_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet

        self.loglist.clear()
        oprationSQL = OprationSQLite()
        result, message = oprationSQL.DeleteTable()
        if result == True:
            self.ThreadRead.start()
            self.btnspider.setEnabled(False)
        #     p = subprocess.Popen(cmd,
        #                          shell=True,
        #                          stdout=subprocess.PIPE,
        #                          stderr=subprocess.STDOUT)
        #
        #     while p.poll() is None:
        #         line=p.stdout.readline()
        #         print(line)
        #         line=line.strip()
        #         QApplication.processEvents()
        #         if line:
        #             cursor.insertText(str(line))
        #
        #     # for line in iter(p.stdout.readline()):
        #     #     print(str(line))
        #     #     cursor.insertText(str(line))
        #
        #     p.stdout.close()
        #     retval = p.wait()
        #
        #     if p.returncode != 0:
        #         cursor.insertText('ERROR')
        #         return -1
        # else:
        #     cursor.insertText(message)

    def slotAdd(self,txtMessage):
        self.loglist.addItem(txtMessage)
        if txtMessage=='爬取【天天基金网】基金排行成功！':
            self.btnspider.setEnabled(True)
    
    @pyqtSlot()
    def on_btnStop_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        self.ThreadRead.terminate()
        self.btnspider.setEnabled(True)
        self.loglist.addItem('爬取【天天基金网】基金排行线程被用户中止！')
        #QApplication.processEvents()

    @pyqtSlot()
    def on_btnQuery_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        operatrionSQLite=OprationSQLite()
        result,rsData=operatrionSQLite.SelectTalbe()

        if result==True:
            showData=showTableWidgets()
            showData.show_tablewidget(rsData,self.twDetails)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = frmSpider()
    ui.show()
    sys.exit(app.exec_())
    
